package jsave.action;

import java.io.File;

import jsave.exception.JSaveArretSauvegarde;
import jsave.exception.JSaveException;
import jsave.tree.FileNode;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class ActionSupprimer extends Action {

	private static final Logger log = Logger.getLogger(ActionSupprimer.class);

	public ActionSupprimer(int typeAction) {

		super();

		if (typeAction == Action.ACTION_A || typeAction == Action.ACTION_B) {
			this.typeAction = typeAction;
		} else {
			log.log(Level.ERROR, "ERREUR - Le type d'action '" + typeAction
					+ "' ne correspond pas a l'action 'ActionSupprimer' ");
		}
	}

	public void executer(File s, File d) throws JSaveArretSauvegarde {

		if (typeAction == ACTION_A) {
			if (s.isDirectory())
				supprimerDossier(s);
			else
				supprimerFichier(s);
		} else {
			if (d.isDirectory())
				supprimerDossier(d);
			else
				supprimerFichier(d);
		}
	}

	public String getNom() {
		return "SUPPRIMER";
	}

	@Override
	public void executer(String sourcePath, String destinatinoPath, FileNode n)
			throws JSaveException {

		if (typeAction == ACTION_B) {
			File f = new File(destinatinoPath + File.separator
					+ n.getRelativePath());
			if (f.exists()) {
				if (n.isDirectory()) {
					// log.info("%%% Suprresion de " + f);
					supprimerDossier(f);
				} else {
					supprimerFichier(f);
				}
			}
		} else {
			throw new JSaveException("Methode non implementee");
		}
	}
}
